package com.jhgsys.internal.sypq.controller;

import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.DateUtil;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.sypq.entity.ModelAttach;
import com.jhgsys.internal.sypq.entity.ModelOars;
import com.jhgsys.internal.sypq.params.ModelAttachAddParams;
import com.jhgsys.internal.sypq.service.IModelAttachService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.NotBlank;
import java.util.Date;
import java.util.Random;

/**
 * 附体 Controller
 *
 * @author ganjun
 * @date 2024-11-20 17:18:55
 */
@Slf4j
@Validated
@Controller
public class ModelAttachController extends BaseController {

    @Autowired
    private IModelAttachService modelAttachService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "modelAttach")
    @RequiresPermissions("modelAttach:view")
    public ModelAndView modelAttachIndex(@RequestParam(name="projectName", required=false, defaultValue="") String projectName){
        String viewPath = jumpPage("sypq/modelAttach/modelAttachList");
        ModelAndView modelAndView = new ModelAndView(viewPath);
        modelAndView.addObject("projectName", projectName);
        return modelAndView;

    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "modelAttach/add")
    @RequiresPermissions("modelAttach:add")
    public String modelAttachAdd(Model model){
        String dateStr = DateUtil.getDateFormat(new Date(), "yyyyMM");
        Random random = new Random();
        int randomNumber = random.nextInt(10000); // 生成0到9999之间的随机数
        String ftNo = "FT" + dateStr + String.format("%04d", randomNumber); // 格式化为四位数字
        model.addAttribute("ftNo", ftNo);

        //项目列表
        model.addAttribute("projList", modelAttachService.getProjectList());
        //桨模实体
        ModelAttach modelAttach = new ModelAttach();
        model.addAttribute("modelAttach", modelAttach);
        return jumpPage("sypq/modelAttach/modelAttachEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "modelAttach/update/{id}")
    @RequiresPermissions("modelAttach:edit")
    public String modelAttachUpdate(Model model, @PathVariable("id") String id){
        //附件信息
        model.addAttribute("attachs", modelAttachService.getModelShipAttachsByAttachId(id, "3"));
        //附体信息
        model.addAttribute("modelAttach", modelAttachService.findModelAttachById(id));
        //项目列表
        model.addAttribute("projList", modelAttachService.getProjectList());
        return jumpPage("sypq/modelAttach/modelAttachEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "modelAttach/finish/{id}")
    @RequiresPermissions("modelAttach:finish")
    public String modelAttachFinish(Model model, @PathVariable("id") String id){
        //附件信息
        model.addAttribute("attachs", modelAttachService.getModelShipAttachsByAttachId(id, "6"));
        model.addAttribute("modelAttach", modelAttachService.findModelAttachById(id));
        return jumpPage("sypq/modelAttach/modelAttachFinish");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "modelAttach/detail/{id}")
    @RequiresPermissions("modelAttach:view")
    public String modelAttachDetail(Model model, @PathVariable("id") String id){
        //附件信息
        model.addAttribute("attachs", modelAttachService.getModelShipAttachsByAttachId(id, ""));
        model.addAttribute("modelAttach", modelAttachService.findModelAttachById(id));
        return jumpPage("sypq/modelAttach/modelAttachDetail");
    }

    /**
     * 查询（所有）
     * @author ganjun
     * @date 2024-11-20 17:18:55
     */
    @GetMapping("modelAttach")
    @ResponseBody
    @RequiresPermissions("modelAttach:view")
    public FebsResponse getAllModelAttachs(ModelAttach modelAttach) {
        try {
            return success(modelAttachService.findModelAttachList(modelAttach));
        }catch (Exception ex){
            return failure(ex);
        }

    }

    /**
     * 查询（分页）
     * @author ganjun
     * @date 2024-11-20 17:18:55
     */
    @GetMapping("modelAttach/list")
    @ResponseBody
    @RequiresPermissions("modelAttach:view")
    public FebsResponse modelAttachList(QueryRequest request, @Param("demondFinishTime") String demondFinishTime, @Param("name") String name, @Param("statusSearch") String statusSearch, @Param("attachCodeSearch") String attachCodeSearch) {
        try {
            Map<String, Object> dataTable = getDataTable(this.modelAttachService.findModelAttachPage(request, demondFinishTime, name, statusSearch, attachCodeSearch));
            return success(dataTable);
        }catch (Exception ex){
            return failure(ex);
        }
    }

    /**
     * 导出
     * @author ganjun
     * @date 2024-11-20 17:18:55
     */
    @GetMapping("modelAttach/export")
    @ResponseBody
    @RequiresPermissions("modelAttach:view")
    public void export(QueryRequest request, @Param("demondFinishTime") String demondFinishTime, @Param("name") String name, @Param("statusSearch") String statusSearch, @Param("attachCodeSearch") String attachCodeSearch, HttpServletResponse response) throws FebsException {
        try {
            List<ModelAttach> list = this.modelAttachService.export(request, demondFinishTime, name, statusSearch, attachCodeSearch);
            ExcelKit.$Export(ModelAttach.class, response).downXlsx(list, false);
        }catch (Exception ex){
            String message = "导出Excel失败";
            log.error(message, ex);
            throw new FebsException(message);
        }
    }

    /**
     * 新增 ModelAttach
     * @author ganjun
     * @date 2024-11-20 17:18:55
     */
    @Log("新增ModelAttach")
    @PostMapping("modelAttach")
    @ResponseBody
    @RequiresPermissions("modelAttach:add")
    public FebsResponse addModelAttach(@Valid @RequestBody ModelAttachAddParams modelAttachAddParams) throws FebsException {
        try {
            this.modelAttachService.createModelAttach(modelAttachAddParams);
            return success();
        } catch (Exception ex) {
            return failure(ex,"新增失败");
        }
    }

    /**
     * 逻辑删除 ModelAttach
     * @author ganjun
     * @date 2024-11-20 17:18:55
     */
    @Log("逻辑删除ModelAttach")
    @GetMapping("modelAttach/delete/{id}")
    @ResponseBody
    @RequiresPermissions("modelAttach:delete")
    public FebsResponse deleteModelAttach(@PathVariable("id") String modelAttachIds) throws FebsException {
        try {
            ModelAttach modelAttach = new ModelAttach();
            modelAttach.setDeletedBy(getCurrentUser().getUserName());
            modelAttach.setDeletedTime(new Date());
            modelAttach.setDeletedFlg(FebsConstant.INVALID);
            this.modelAttachService.deleteModelAttach(modelAttach,modelAttachIds);
            return success();
        } catch (Exception ex) {
            return failure(ex,"删除失败");
        }
    }

    /**
    * 物理删除 ModelAttach
    * @author ganjun
    * @date 2024-11-20 17:18:55
    */
    @Log("物理删除ModelAttach")
    @GetMapping("modelAttach/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("modelAttach:delete")
    public FebsResponse physicallyDeleteModelAttach(@PathVariable("id") String modelAttachIds) throws FebsException {
        try {
            this.modelAttachService.physicallyDeleteModelAttach(modelAttachIds);
            return success();
        } catch (Exception ex) {
            return failure(ex,"删除失败");
        }
    }

    /**
     * 修改 ModelAttach
     * @author ganjun
     * @date 2024-11-20 17:18:55
     */
    @Log("修改ModelAttach")
    @PostMapping("modelAttach/update")
    @ResponseBody
    @RequiresPermissions("modelAttach:edit")
    public FebsResponse updateModelAttach(@RequestBody ModelAttachAddParams modelAttachAddParams) throws FebsException {
        try {
            this.modelAttachService.updateModelAttach(modelAttachAddParams);
            return success();
        } catch (Exception ex) {
            return failure(ex,"修改失败");
        }
    }

    /**
     * 完成 modelAttach
     * @author ganjun
     * @date 2024-11-18 22:28:43
     */
    @Log("完成modelAttach")
    @PostMapping("modelAttach/finish")
    @ResponseBody
    @RequiresPermissions("modelAttach:finish")
    public FebsResponse finishModelOars(@Valid @RequestBody ModelAttachAddParams params) throws FebsException {
        try {
            this.modelAttachService.finishModelAttach(params);
            return success();
        } catch (Exception ex) {
            return failure(ex,"完成操作失败");
        }
    }

}
